<?php
class product extends head2d {
 public $showtype = 1;

 function product() {
  $this->table = "PRODUCT";
  $this->linetable = "PRODUCTBUILDS";
  $this->getmetadata();
  $this->maintainhist = false;
  $this->header[2] = "";
  $_SESSION[$this->table] = $this;
 }

 function defaultline() {
  $ans = array_fill(0,$this->lineatts,"");
  $ans[3] = "1";
  $ans[1] = sizeof($this->lines);
  $ans[2] = "";
  return $ans;
 }
 

 function applyrefs() {
  $this->valids = array_fill(0,$this->atts,"");
  $this->lists = array_fill(0,$this->atts,"");
  $this->refs = array_fill(0,$this->atts,"");
  if ($this->header[5] !== "") {
    $desc = freaditem("SUPPLIER",$this->header[5]);
    $this->refs[5] = $desc[1];
  }
  if ($this->header[8] !== "") {
    $desc = freaditem("GST",$this->header[8]);
    $this->refs[8] = $desc[1];
  }
 }
 
 function select() {
  echo "<h1>Choose Product</h1>";
  parent::selectlist("");
 }
 
 function checknonrecursive($f,$toofar = 1) {
  if ($f == $this->header[0]) return 0 ;
  if ($toofar > 20) return 0;
  $sql = "select subproduct from productbuilds where itemid = '$f'";
  $list = sqlreadkeyarray($sql);
  if ($list) {
   foreach ($list as $productid=>$item) {
    if ($productid == $this->header[0]) {return 0 ;}
    if ($this->checknonrecursive($productid,$toofar+1) == 0) {return 0 ;}
   }
  }
  return "1";
 }
 
 function createblank() {
  $this->header = array_fill(0,$this->atts,'');
  $this->header[2] = '0';
  $this->header[13] = '0';
  $this->header[8] = '1';
  $this->header[10] = '0';
  $this->header[11] = '0';
  $this->header[12] = '0';
  $this->header[13] = '0';
  
  $this->valids = array_fill(0,$this->atts,'');
  $this->lists = array_fill(0,$this->atts,'');
  $this->refs = array_fill(0,$this->atts,'');
  $this->currentlinerefs = array_fill(0,$this->lineatts,'');
  $this->linerefs = null;

 }



function drawbom($prod,$level,$downparent) {
 $boxwidth=150 ;
 global $drawdown;
 global $spans;
 if ($downparent == 0) {
  $list = "";
  foreach($this->lines as $item) {
   $list[][0] = $item[2];
  }
 } else {
  $sql = "select subproduct from productbuilds where itemid = '$prod'";
  $list = sqlreadarray($sql);
 }
 echo ("jg_doc.setColor('#D6E9F8')\n");
 echo ("jg_doc.fillRect($level*$boxwidth+15,$drawdown,$boxwidth-30,19)\n");
 echo ("jg_doc.setColor('#7f7f7f')\n");
 echo ("jg_doc.drawRect($level*$boxwidth+15,$drawdown,$boxwidth-30,18)\n");
 if ($downparent != 0) {
  echo ("jg_doc.setColor('#7f7f7f')\n");
  echo ("jg_doc.drawLine(($level-1)*$boxwidth+$boxwidth-15,$downparent+7,$level*$boxwidth+15,$drawdown+7)\n");
 }
 echo ("jg_doc.setColor('#000000')\n");
 $sql = "select process.itemid from process,product where product.itemid = '$prod' and product.processid = process.itemid";
 $processitem = sqlread($sql);
 $processname = substr($processitem[0],0,4);
 echo "jg_doc.drawStringRect('$prod ($processname)',$level*$boxwidth,$drawdown+2,$boxwidth,'center')\n";
 $downparent = $drawdown;
 if ($list) {
   foreach ($list as $productid) {
    $this->drawbom($productid[0],$level+1,$downparent);
    $drawdown = $drawdown + 20;
   }
   $drawdown = $drawdown - 20;
 }
}



function drawit() {
global $drawdown ;
global $spans;
echo "<script src='wz_jsgraphics.js'></script>";
echo "<div style='position:absolute; left:10px; top:150px;' id = 'canvas'></div>";
echo "<script>";
echo "var jg_doc = new jsGraphics('canvas')\n";
echo "jg_doc.setFont('arial', '11px', Font.ITALIC)\n"; 
$prod = 'PRODUCT'; 
$drawdown = 2 ;
$this->drawbom($prod,0,0);
echo "jg_doc.paint();\n";
echo "</script>"; 
echo $spans;
}






 function applyfromrequest() {
  // Mapped requests f1-> field 1 etc
  $this->valid = true;
  $this->valids = array_fill(0,$this->atts,'');
  $this->currentlinevalids = array_fill(0,$this->lineatts,'');

  
  if (isset($_REQUEST['f0']) && $this->header[2] == '0') {
   $size = $this->metadata[0]->length ;  
   $f = $_REQUEST['f0'];
   if(preg_match("/^[0-9A-Z\.-]{0,$size}$/",$f)) {
    $exists = freaditem($this->table,$f) ;
    if ($exists == '') {
     $this->header[0] = $f;
    } else {
     $this->valids[0] = 'That ID is already in use for '.$exists[1];
    }
   } else {
    $this->valids[0] = "Product code should be made up of numbers and uppercase letters only";
   }
  }
  
  $p = 2; 
	if($f=@$_REQUEST["l$p"]) {   
    // Product code
    $productitem = freaditem('product',$f);
    if ($productitem == '') {
     $this->currentlinevalids[$p] = 'Need valid product code';
     $this->currentlinelists[$p] = $this->selectlisth('PRODUCT',$f,'l2');
    } else {
     // Need to check there is no recursion. The parent is not included below this path.
     if ($this->checknonrecursive($f)) {
      $this->currentline[$p] = $f;
      $this->currentlinerefs[$p] = $productitem[1];
      $this->currentlinelists[$p] = '';
     } else {
      $this->currentlinevalids[$p] = 'This would cause a recursive call';
     }
    }
  }
  
  $p = 3;
  if ($f=@$_REQUEST["l$p"]) {
    // Quantity

   $size = $this->linemetadata[$p]->length ;
   $dec  = $this->linemetadata[$p]->decimals;
   $len  = $size-$dec;
   if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,$dec}$/",$f)) {
    $this->currentline[$p] = $f;
   } else {
    $this->currentlinevalids[$p] = 'Please enter an amount in currency';
   }

  }

  $i=5;
	if ($f=@$_REQUEST["f$i"]) {
   // Code of Supplier object
   $desc = freaditem('SUPPLIER',$f);
   if ($desc == '') {
    $this->valids[$i] = 'Need valid supplier code';
    $this->lists[$i] = $this->selectlisth('SUPPLIER',$f,"f$i");
   } else {
    $this->header[$i] = $f;
    $this->refs[$i] = $desc[1];
    $this->lists[$i] = '';
   }
  }

  $i = 6;
	if ($f=@$_REQUEST['f6']) {
   // Code of Group object
   $desc = freaditem('PGROUP',$f);
   if ($desc == '') {
    $this->valids[$i] = 'Need valid group code';
    $this->lists[$i] = $this->selectlisth('PGROUP',$f,'f'.$i);
   } else {
    $this->header[$i] = $f;
    $this->refs[$i] = $desc[1];
    $this->lists[$i] = '';
   }
  }
  
  $i = 18;
	if ($f=@$_REQUEST["f$i"]) {
	 if ($f==" ") {
	  $this->header[$i] = "";
	  $this->refs[$i] = "";
	 } else {
   // Code of Process object
   $desc = freaditem('PROCESS',$f);
   if ($desc == '') {
    $this->valids[$i] = 'Need valid process code';
    $this->lists[$i] = $this->selectlisth('PROCESS',$f,'f'.$i);
   } else {
    $this->header[$i] = $f;
    $this->refs[$i] = $desc[1];
    $this->lists[$i] = '';
   }
   }
  }
  
/*
  if ($f=@$_REQUEST['f8']) {
   $desc = freaditem('GST',$f);
   if ($desc == '') {
    $this->valids[8] = 'Need valid gst code';
    $this->lists[8] = $this->selectlisth('GST',$f,'f8');
   } else {
    $this->header[8] = $f;
    $this->refs[8] = $desc[1];
    $this->lists[8] = '';
   }
  }
*/
  
  if( $f=@$_REQUEST["f1"]) {
   $size = $this->metadata[1]->length ;
   $a = preg_replace("/[^A-Z\na-z0-9!@#$%&*\/()+?\-\']/"," ",$f);
   $a = substr($a,0,$size);
   $this->header[1] = $a;
  }

  $i = 19;
	if( $f=@$_REQUEST["f$i"]) {
   $size = $this->metadata[$i]->length ;
   $dec  = $this->metadata[$i]->decimals;
   $len  = $size-$dec;
   if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,$dec}$/",$f)) {
    $this->header[$i] = $f;
   } else {
    $this->valids[$i] = 'Please enter an amount in minutes';
   }
  } 
  
/*
  if( $f=@$_REQUEST['f11']) {
   $size = $this->metadata[11]->length ;
   $dec  = $this->metadata[11]->decimals;
   $len  = $size-$dec;
   if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,$dec}$/",$f)) {
    $this->header[11] = $f;
   } else {
    $this->valids[11] = 'Please enter an amount in currency';
   }
  } 
  
  if( $f=@$_REQUEST['f13']) {
   $size = $this->metadata[13]->length ;
   $dec  = $this->metadata[13]->decimals;
   $len  = $size-$dec;
   if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,$dec}$/",$f)) {
    $this->header[13] = $f;
   } else {
    $this->valids[13] = 'Please enter an amount in currency';
   }
  } 
  
  
  if ($this->header[13] == '') {
   $this->valids[13] = 'Cannot leave blank';
  }

  if ($this->header[11] == '') {
   $this->valids[11] = 'Cannot leave blank';
  }

  if ($this->header[12] == '') {
   $this->valids[12] = 'Cannot leave blank';
  }

  if ($this->header[5] == '') {
   $this->valids[5] = 'Cannot leave supplier blank';
  }

  if ($this->header[8] == '') {
   $this->valids[8] = 'Cannot leave GST blank';
  }
*/
  
  $i = 23;
	if( $f=@$_REQUEST["f$i"]) {
   $size = $this->metadata[$i]->length ;
   $dec  = $this->metadata[$i]->decimals;
   $len  = $size-$dec;
   if(preg_match("/^-{0,1}[0-9]{0,$len}[.]{0,1}[0-9]{0,$dec}$/",$f)) {
    $this->header[$i] = $f;
   } else {
    $this->valids[$i] = 'Please enter an amount in minutes';
   }
  }
	 
	if ($this->currentline[2] == '') {
	 $this->currentlinevalids[2] = 'Cannot leave item code blank';
	} 
  $this->valid = every($this->valids,''); 

  // Add a line
   if ($apply = @$_REQUEST['sub']) {
    if ($apply == 'Complete Line' || $apply == 'Apply') {
      if (every($this->currentlinevalids,'') ) {
       $this->lines[$this->currentline[1]] = $this->currentline;
       $this->linerefs[$this->currentline[1]] = $this->currentlinerefs;
       $this->currentline = $this->defaultline();
      } else {
       $this->valid = false;
      }
     }
    } elseif ($apply == 'Abandon Changes') {
     $this->showtype = 1;
     $this->currentline = $this->defaultline();
    }

   // Unset if requested. Note that unset is slow
   (isset($_REQUEST['x'])) ? $x=$_REQUEST['x'] : $x = -1;
   if (isset($this->lines[$x])) {
    unset($this->lines[$x]);
    $this->lines = array_values($this->lines); // Removes blanks
    $this->currentline = $this->defaultline();
   }
   
	 if (isset($_REQUEST['editline']) && (every($this->currentlinevalids,'') || $this->currentline==$this->defaultline())) {
	  $e = $_REQUEST['editline'];
	  if (isset($this->lines[$e])) {
	   $this->currentline = $this->lines[$e];
	   $this->currentline[1] = $e;
     $this->currentlinevalids = array_fill(0,$this->lineatts,'');
	  }
	 }   
 

  if($this->valid) {
	  if ($f=@$_REQUEST['showtype']) {
     $this->showtype=$f;
    }
  }
 }
 
 function showproductbuilds() {

	echo '<table width=60%>';
	echo '<tr><th>Actions</th><th>Product Code</th><th width=70%>Description</th><th>Qty</th></tr>';
	for ($i=0;$i<=sizeof($this->lines);$i++) {
	 if ($this->currentline[1] == $i && $this->disabled != 'disabled') {
	  // Show the line we are editing.
  	echo "<tr>\n";
   	echo "<td>Edit Line</td>";
 	  if ($this->currentlinelists[2] == '') {
     echo "<td><input onfocus='dofocus()' id=idl5 type=text size=10 maxlength=16 name='l2' value='".$this->currentline[2]."'></td>\n";	
  	 if ($this->currentline[2] !== '') {$disable = 'readonly=readonly';} else {$disable='';}
     echo "<td>{$this->currentlinerefs[2]}</td>";
    } else {
     echo "<td colspan=2>".$this->currentlinelists[2]."</td>";
    }
    $size = $this->linemetadata[3]->length ;

	  echo "<td><input id=idl3 type=text size=5 maxlength=$size name='l3' value=''.$this->currentline[3].''></td>\n";	
	  echo "<td></td>";
	  echo "</tr>";
	} elseif (isset($this->lines[$i])) {
    // Show other lines
	  echo '<tr>';
		echo '<td>';
	  echo "<img title='Edit line' onclick='document.form.showtype.value=4;submitform1($i);' src='images/e.gif'>"; 	
	  if ($this->disabled != 'disabled') {
     echo "<img title='Delete line' onclick=submitform2($i) src='images/x.gif'>";
    }
	  echo '</td>';
	  echo '<td>'.$this->lines[$i][2].'&nbsp;</td>';
    echo "<td>{$this->linerefs[$i][2]}</td>";	  
	  echo '<td align=right>'.$this->lines[$i][3].'&nbsp;</td>';
	  echo "</tr>\n";   
	 }
	}
	echo '</table>';
  echo '<input class=bt accesskey=A type=submit name=sub value=Apply>';
  echo '<input class=bt accesskey=S type=submit name=sub value=Save>';
  echo '<input class=bt accesskey=P type=submit name=sub value=Print>';
  echo '<input class=bt accesskey=E type=submit name=sub value=Email>';
  echo '<button class=bt onclick=rest()><span>Restart</span></button>'; 
  
 }
 
 function showform() {
  $readonly = 'readonly'; $focalpoint = 'f1';
  if ($this->showtype=='2') $focalpoint = 'l2';
  if ($this->header[2] == '0') { $focalpoint = 'f0'; $readonly = '';} 
 
  echo '<head>';
  echo SCRIPTS;
  echo '</head>';
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
  echo "<h1>Product Setup</h1>"; 
  echo "<form name='form' method=GET>";
  echo "<input type=hidden name=res value=''>";
  echo "<input type=hidden name=showtype value=$this->showtype>";
  echo "<input type=hidden name=editline value=-1>";
  echo "<input type=hidden name=x value=-1>\n";
  // tabs
  echo "<table border=0 cellpadding=0 frame=void><tr>";
  echo "<td><a accesskey='l' onclick='document.form.showtype.value=2;document.form.submit();'>Builds</a></td>";
  echo "<td><a accesskey='n' onclick='document.form.showtype.value=1;document.form.submit();'>Header</a></td>";
  echo "<td><a accesskey='h' onclick='document.form.showtype.value=3;document.form.submit();'>Show Diagram</a></td>";
  echo "</tr></table>";

  echo "<br>";
  echo "<table>";

   if ($this->header[2] == '0') {
    echo $this->htmlrow('0','Code'); 
   } else {
    echo "<tr>";
    echo " <td>Product Item Code</td>";
    echo " <td>".$this->header[0]."</td>";
    echo "</tr>";
   }

  
  if ($this->showtype=='1') {
   echo $this->htmlrow('1','Description');
   echo $this->htmlrow('5','Supplier');
   echo $this->htmlrow('6','Product Group');
   echo $this->htmlrow('18','Process');
   echo $this->htmlrow('23','Process time in minutes');
   echo "</table>";
   echo "<input accesskey=A class=bt type=submit name=sub value=Apply>";
   echo "<input accesskey=S class=bt type=submit name=sub value=Save>";
   echo "<button class=bt onclick=rest()><span>Restart</span></button>";
   echo "</form>";
  } elseif ($this->showtype=='3') {
   $this->drawit();
  } else {  
	 $this->showproductbuilds(); 
   echo "</form>";
  }
  // set up help
  echo $this->registerhelp(); 
 }
 
}
?>